﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Data;
using Workflow.Alerts.DAL;
using Workflow.Alerts.Interfaces.ProfileSettings;

namespace Workflow.Alerts
{
    public class ContractorDtlsPresenter : Presenter
    {
        #region members
        private readonly ContractorDAL model;
        private readonly IContractorDtlsView view;
        #endregion

        #region constructor
        public ContractorDtlsPresenter(IContractorDtlsView _view)
            : this(_view, null)
        { }

        public ContractorDtlsPresenter(IContractorDtlsView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IContractorDtlsView>();
            view.OnPageLoad += view_OnLoad;
            view.OnUpdate += view_OnUpdate;
            model = new ContractorDAL();
        }

        #endregion

        #region private methods

        private void view_OnLoad()
        {
            var filter = new WorkflowFilter
            {
                ContractorId = view.Id,
                MunicipalityId = CurrentUser.MunicipalityId
            };

            var filterSql = new WorkflowFilterSql(filter);

            EstateDataSet.ContractorDataTable operators = model.GetContractorDetails(filterSql);

            if (operators.Rows.Count == 1)
            {
                EstateDataSet.ContractorRow Contractor = operators[0];
                view.Name = Contractor.name;                
                view.PostAddress = Contractor.postaddress;
                view.PostCode = Contractor.postcode;
                view.City = Contractor.postcity;
                view.IsDisabled = Contractor.disabled;                
                view.Email = Contractor.email;
                view.FirstName = Contractor.firstname;
                view.LastName = Contractor.lastname;
                view.Phone1 = Contractor.phone1;
                view.Phone2 = Contractor.phone2;
            }
        }

        private bool view_OnUpdate()
        {
            var filter = new WorkflowFilter
            {
                Name = view.Name,
                PostAddress = view.PostAddress,
                PostCode = view.PostCode,
                PostCity = view.City,
                ContractorId = view.Id,
                Email = view.Email,
                Disabled = view.IsDisabled,
                MunicipalityId = CurrentUser.MunicipalityId,
                LastName = view.LastName,
                FirstName = view.FirstName,
                Phone1 = view.Phone1,
                Phone2 = view.Phone2

            };

            WorkflowFilterSql filterSql = new WorkflowFilterSql(filter);

            PersonDAL dal = new PersonDAL();
            string exceptionField = string.Empty;
            if (!dal.ValidateUser(filterSql, ref exceptionField))
            {
                view.ExceptionField = exceptionField;
                return false;
            }

            if (view.Id.HasValue)
            {
                //update existing operator
                model.UpdateContractor(filterSql);
            }
            else
            {
                //create new operator                
                view.Id = model.CreateContractor(filterSql);
            }

            return true;
        }
        #endregion
    }
}
